package cn.pku.eecs.ldn.view;

import java.util.HashMap;
import java.util.Map;

import cn.pku.eecs.ldn.R;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class SelectImagesGridView extends GridView {
	private static final String TAG = "SelectImagesGridView";
	private static final boolean DEBUG = true;

	public SelectImagesGridView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public SelectImagesGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public SelectImagesGridView(Context context) {
		super(context);
		init();
	}

	ImagesAdapter imagesAdapter = new ImagesAdapter();

	private void init() {
		setAdapter(imagesAdapter);
		setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ImageView imageViewHighlight = (ImageView) arg1
						.findViewById(R.id.imageViewHightlight);
				if (imageViewHighlight != null) {
					if (DEBUG)
						Log.d(TAG, "onItemClick(): selected status set from "
								+ selectedStatus[arg2] + " to "
								+ !selectedStatus[arg2]);
					if (selectedStatus[arg2]) {
						selectedStatus[arg2] = false;
						imageViewHighlight.setVisibility(View.GONE);
					} else {
						selectedStatus[arg2] = true;
						imageViewHighlight.setVisibility(View.VISIBLE);
					}
				} else {
					if (DEBUG)
						Log.e(TAG, "onItemClick(): imageViewHighlight is null");
				}

			}

		});
		setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				if (DEBUG)
					Log.i(TAG, "onItemSelected()");
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				if (DEBUG)
					Log.i(TAG, "onNothingSelected()");
			}
		});
	}

	public void test() {
		// imagePaths = new String[] { "1", "2", "3", "4", "5", "6", "7", "8",
		// "9", "10", "11", "12", "13", "14", "15", "16", "17", "18" };
		imagePaths = new String[] { "1" };
		int length = imagePaths.length;
		selectedStatus = new boolean[length];
		for (int i = 0; i < length; i++) {
			selectedStatus[i] = false;
		}
		imagesAdapter.notifyDataSetChanged();
	}

	String[] imagePaths = null;
	boolean[] selectedStatus = null;

	class ImagesAdapter extends BaseAdapter {

		Map<String, View> viewCache = new HashMap<String, View>(0);

		@Override
		public int getCount() {
			if (imagePaths == null) {
				return 0;
			}
			return imagePaths.length;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (DEBUG)
				Log.d(TAG, "getView(): " + position);
			String image = imagePaths[position];
			convertView = viewCache.get(image);
			if (convertView == null) {
				if (DEBUG)
					Log.d(TAG, "getView(): cache missed.");
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.item_select_images_gridview, null);
				ImageView imageView = (ImageView) convertView
						.findViewById(R.id.imageView);
				imageView.setImageResource(getImageResID(position));
				viewCache.put(image, convertView);
			} else {
				if (DEBUG)
					Log.d(TAG, "getView(): cache hit.");
			}
			ImageView imageViewHighlight = (ImageView) convertView
					.findViewById(R.id.imageViewHightlight);
			if (selectedStatus[position]) {
				imageViewHighlight.setVisibility(View.VISIBLE);
			} else {
				imageViewHighlight.setVisibility(View.INVISIBLE);
			}

			return convertView;
		}
	}

	private int getImageResID(int position) {
		position %= 5;
		switch (position) {
		case 0:
			return R.drawable.girl;
		case 1:
			return R.drawable.flower;
		case 2:
			return R.drawable.naeun;
		case 3:
			return R.drawable.sky;
		case 4:
			return R.drawable.splash_logo;

		default:
			return R.drawable.long_image;
		}
	}

}
